package com.meetingshow.weibo.uiservice;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.validation.groups.Default;

import org.apache.log4j.Logger;

import com.meetingshow.common.base.service.model.QueryResults;
import com.meetingshow.common.base.uiservice.BaseUIServiceImp;
import com.meetingshow.common.base.uiservice.exception.BaseUIServiceException;
import com.meetingshow.common.validator.RemoveValidator;
import com.meetingshow.common.validator.UpdateValidator;
import com.meetingshow.utils.BaseUtil;
import com.meetingshow.weibo.dao.model.WeiboPO;
import com.meetingshow.weibo.service.IWeiboService;
import com.meetingshow.weibo.service.model.WeiboQueryParas;
import com.meetingshow.weibo.uiservice.model.WeiboQueryParasVO;
import com.meetingshow.weibo.uiservice.model.WeiboVO;

public class UIWeiboServiceImp extends BaseUIServiceImp implements
        IUIWeiboService {
    
    public final static Logger logger = Logger.getLogger(UIWeiboServiceImp.class);

    private IWeiboService weiboService;
    
    /**
     * @return the weiboService
     */
    public IWeiboService getWeiboService() {
        return weiboService;
    }

    /**
     * @param weiboService the weiboService to set
     */
    public void setWeiboService(IWeiboService weiboService) {
        this.weiboService = weiboService;
    }

    @Override
    public Integer saveWeibo(WeiboVO weiboVO)
            throws BaseUIServiceException {
        
        if (!BaseUtil.objectNotNull(weiboVO)) {
            this.throwValidaterException("weiboVO is null.");
        }
        
        weiboVO.setCreateTime(new Date());
        
        validateThrowEx(weiboVO);
        
        WeiboQueryParas weiboQueryParas = new WeiboQueryParas();
        weiboQueryParas.setUserId_e(String.valueOf(weiboVO.getUserId()));
        //weiboQueryParas.setWeiboType_e(weiboVO.getWeiboType());
        
        QueryResults<WeiboPO> wbQueryResults = null;
        
        try {
            wbQueryResults = getWeiboService().queryWeibo(weiboQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (QueryResults.resultsListNotNull(wbQueryResults)) {
            logger.error("Weibo of this user exists.");
            throw new BaseUIServiceException("Weibo of this user exists.");
        }
        
        try {
            Integer id = getWeiboService().saveWeibo(weiboVO.toWeiboPO());
            weiboVO.setId(id);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        return weiboVO.getId();
    }

    @Override
    public void updateWeibo(WeiboVO weiboVO)
            throws BaseUIServiceException {
        if (!BaseUtil.objectNotNull(weiboVO)) {
            this.throwValidaterException("weiboVO is null.");
        }
        
        validateThrowEx(weiboVO, Default.class, UpdateValidator.class);
        
        WeiboQueryParas weiboQueryParas = new WeiboQueryParas();
        weiboQueryParas.setId_e(String.valueOf(weiboVO.getId()));
        
        QueryResults<WeiboPO> weiboQueryResults = null;
        try {
            weiboQueryResults = getWeiboService().queryWeibo(weiboQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (QueryResults.resultsListNotNull(weiboQueryResults)) {
            WeiboPO persistWeiboPO = weiboQueryResults.getResultList().get(0);
            persistWeiboPO.setAccessToken(weiboVO.getAccessToken());
            persistWeiboPO.setAccessTokenSecret(weiboVO.getAccessTokenSecret());
            persistWeiboPO.setWeiboId(weiboVO.getWeiboId());
            persistWeiboPO.setWeiboType(weiboVO.getWeiboType());
            
            //验证是否已经绑定
            weiboQueryParas = new WeiboQueryParas();
            weiboQueryParas.setUserId_e(weiboVO.getWeiboId());
            //weiboQueryParas.setWeiboType_e(weiboVO.getWeiboType());
            
            weiboQueryResults = null;
            try {
                weiboQueryResults = getWeiboService().queryWeibo(weiboQueryParas);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                throw new BaseUIServiceException(e);
            }
            
            if (QueryResults.resultsListNotNull(weiboQueryResults)
                    && !weiboVO.getId().equals(
                            weiboQueryResults.getResultList().get(0).getId())) {
                logger.error("Weibo of this user exists.");
                throw new BaseUIServiceException("Weibo of this user exists.");
            }
            
            try {
                getWeiboService().updateWeibo(persistWeiboPO);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                throw new BaseUIServiceException(e);
            }
        }
    }

    @Override
    public void removeWeibo(WeiboVO weiboVO)
            throws BaseUIServiceException {
        if (!BaseUtil.objectNotNull(weiboVO)) {
            this.throwValidaterException("weiboVO is null.");
        }
        
        validateThrowEx(weiboVO, RemoveValidator.class);
        
        WeiboQueryParas weiboQueryParas = new WeiboQueryParas();
        weiboQueryParas.setId_e(String.valueOf(weiboVO.getId()));
        
        QueryResults<WeiboPO> weiboQueryResults = null;
        try {
            weiboQueryResults = getWeiboService().queryWeibo(weiboQueryParas);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (QueryResults.resultsListNotNull(weiboQueryResults)) {
            WeiboPO persistWeiboPO = weiboQueryResults.getResultList().get(0);
            try {
                getWeiboService().removeWeibo(persistWeiboPO);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                throw new BaseUIServiceException(e);
            }
        }
    }
    
    @Override
    public WeiboVO loadWeiboById(Integer id) throws BaseUIServiceException {
        if (id == null)
            this.throwValidaterException("id is null.");
        
        WeiboPO weiboPO = null;
        try {
            weiboPO = getWeiboService().loadWeiboById(id);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        if (weiboPO == null)
            return null;
        else
            return new WeiboVO(weiboPO);
    }

    @Override
    public QueryResults<WeiboVO> queryWeibo(
            WeiboQueryParasVO weiboQueryParasVO)
            throws BaseUIServiceException {
        if (!BaseUtil.objectNotNull(weiboQueryParasVO)) {
            this.throwValidaterException("weiboQueryParasVO is null.");
        }
        
        validateThrowEx(weiboQueryParasVO);
        
        QueryResults<WeiboPO> weiboPOQueryResults = null;
        try {
            weiboPOQueryResults = getWeiboService().queryWeibo(weiboQueryParasVO.toWeiboQueryParas());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseUIServiceException(e);
        }
        
        QueryResults<WeiboVO> weiboVOQueryResults = new QueryResults<WeiboVO>();
        List<WeiboVO> weiboVOList = new ArrayList<WeiboVO>();
        if (QueryResults.resultsListNotNull(weiboPOQueryResults)) {
            for (WeiboPO weiboPO : weiboPOQueryResults.getResultList()) {
                if (BaseUtil.objectNotNull(weiboPO)) {
                    weiboVOList.add(new WeiboVO(weiboPO));
                }
            }
        }
        
        weiboVOQueryResults.setFirstResultNo(weiboPOQueryResults.getFirstResultNo());
        weiboVOQueryResults.setResultsPerGroup(weiboPOQueryResults.getResultsPerGroup());
        weiboVOQueryResults.setResultList(weiboVOList);
        weiboVOQueryResults.setTotalResultsNum(weiboPOQueryResults.getTotalResultsNum());
        
        return weiboVOQueryResults;
    }

}
